import socket
import threading

def receive_messages(client_socket):
    while True:
        try:
            message = client_socket.recv(1024).decode()
            if not message:
                break
            print("Server:", message)
        except:
            break
    print("Disconnected from server.")
    client_socket.close()

def send_messages(client_socket):
    while True:
        message = input()
        if message.lower() == "exit":
            break
        try:
            client_socket.send(message.encode())
        except:
            break
    client_socket.close()

def main():
    host = "127.0.0.1" 
    port = 12345

    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((host, port))
    print("Connected to server.")


    recv_thread = threading.Thread(target=receive_messages, args=(client_socket,))
    send_thread = threading.Thread(target=send_messages, args=(client_socket,))

    recv_thread.start()
    send_thread.start()

    recv_thread.join()
    send_thread.join()

    client_socket.close()

if __name__ == "__main__":
    main()
